<?php
namespace app\farm\controller;
use app\ApiRest;

use app\farm\model\Address;
use app\farm\model\BalanceWater;
use app\farm\model\Claim;
use app\farm\model\ClaimOrder;
use app\farm\model\Config;
use app\farm\model\Coupon;
use app\farm\model\CouponRecord;
use app\farm\model\Evaluate;
use app\farm\model\FinanceWater;
use app\farm\model\LandCate;
use app\farm\model\Farmer;

use app\farm\model\LandCycle;
use app\farm\model\LandList;
use app\farm\model\LandMassif;
use app\farm\model\LandOrder;
use app\farm\model\LandOrderSeed;
use app\farm\model\LandSeed;
use app\farm\model\LandSpe;
use app\farm\model\LandText;
use app\farm\model\Machine;
use app\farm\model\Massif;
use app\farm\model\OrderAddress;
use app\farm\model\Seed;
use app\farm\model\SendOrder;
use app\farm\model\Source;
use app\massage\controller\IndexWxPay;
use app\farm\model\User;
use app\publics\model\TmplConfig;
use app\shop\model\FreightTemplate;
use longbingcore\wxcore\PayModel;
use think\App;
use think\facade\Db;
use think\Request;


class IndexLand extends ApiRest
{

    protected $model;

    protected $cate_model;

    protected $app;


    public function __construct(App $app) {

        parent::__construct($app);

        $this->app =  $app;

        $this->model = new LandList();

        $this->cate_model = new LandCate();

        autoCancelOrder($this->_uniacid);
    }






    /**
     * @author chenniang
     * @DataTime: 2021-12-14 17:42
     * @功能说明:认养分类列表
     */
    public function landCateList(){

        $input = $this->_param;

        $cate_model = new LandCate();

        $dis = [

            'type'    => 1,

            'uniacid' => $this->_uniacid,

            'status'  => 1,

        ];

        $data = $cate_model->where($dis)->order('top desc,id desc')->select()->toArray();

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-14 17:53
     * @功能说明:土地列表
     */
    public function landList(){

        $input = $this->_param;

        $farmer_model = new Farmer();

        $input['sort'] = !empty($input['sort'])?$input['sort']:1;

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        $dis[] = ['a.status','=',1];

        if(!empty($input['cate_id'])){

            $dis[] = ['b.cate_id','=',$input['cate_id']];

            $dis[] = ['b.type','=',2];
        }

        if(!empty($input['title'])){

            $dis[] = ['a.title','like','%'.$input['title'].'%'];

        }

        if(!empty($input['farmer_id'])){

            $dis[] = ['a.farmer_id','=',$input['farmer_id']];

        }else{

            $id = $farmer_model->farmerId($this->_uniacid);

            $dis[] = ['a.farmer_id','in',$id];

        }

        $lat = !empty($input['lat'])?$input['lat']:0;

        $lng = !empty($input['lng'])?$input['lng']:0;

        $alh = '(2 * 6378.137* ASIN(SQRT(POW(SIN(PI()*('.$lng.'- `lng`)/360),2)+COS(PI()*33.07078170776367/180)* COS('.$lat.' * PI()/180)*POW(SIN(PI()*('.$lat.'- lat)/360),2))))*1000 as distance';

        $data = $this->model->indexDataListV2($dis,$alh,$input['sort']);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-16 10:34
     * @功能说明:土地详情
     */
    public function landInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->model->landInfo($dis);

        $farmer_model = new Farmer();
        //农场主消息
        $data['farmer_info'] = $farmer_model->dataInfo(['id'=>$data['farmer_id']],'mobile,title,cover');

        $app_num = 0;

        if(!empty($data['spe'])){

            $order_model = new LandOrder();

            foreach ($data['spe'] as &$v){

                $dis = [

                    'land_id' => $input['id'],

                    'spe_id'  => $v['id']
                ];

                $is_app = $order_model->where($dis)->where('pay_type','>=',1)->find();

                $v['is_app'] = !empty($is_app)?1:0;

                if($v['is_app']==1){

                    $app_num ++;
                }

            }

        }

        $data['spe_info'] = [
            //总数量
            'all_num' => count($data['spe']),
            //已被预约数量
            'app_num' => $app_num

        ];

        $mac_model = new Machine();
        //仪器详情
        $data['machine_info'] = $mac_model->getDataInfo($input['id']);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-02-11 11:24
     * @功能说明:土地预支付信息
     */
    public function landPayOrderInfo(){

        $input = $this->_input;

        $order_model  = new LandOrder();

        $farmer_model = new Farmer();

        $data = $order_model->payOrderInfo($input);

        if(!empty($data['code'])){

            $this->errorMsg($data['msg']);
        }

        $data['farmer_info'] = $farmer_model->dataInfo(['id'=>$data['land']['farmer_id']],'title');

        $address_model  = new Address();

        if(!empty($input['address_id'])){
            //地址
            $data['address']= $address_model->dataInfo(['id'=>$input['address_id']]);
        }else{

            $data['address'] = $address_model->dataInfo(['user_id'=>$this->getUserId(),'status'=>1]);
        }
        //模版消息model
        $tmpl_model = new TmplConfig();
        //获取模版
        $data['tmp_list'] = $tmpl_model->where(['uniacid'=>$this->_uniacid])->where('tmpl_name','in',['land_order','land_over'])->select()->toArray();

        $coupon_modle  = new Coupon();

        $coupon_record_model  = new CouponRecord();
        //可用优惠券数量
        $canUseCoupon = $coupon_modle->canUseCoupon($this->getUserId(),$data['init_price'],2);

        $data['canUseCoupon'] = $coupon_record_model->where('id','in',$canUseCoupon)->sum('num');

        return $this->success($data);
    }



    /**
     * @author chenniang
     * @DataTime: 2021-12-16 10:57
     * @功能说明:土地下单
     */
    public function landPayOrder(){

        $input = $this->_input;

        $address_model  = new OrderAddress();

        $order_model    = new LandOrder();

        $land_seed_model= new LandOrderSeed();

        $pay_order  = $order_model->payOrderInfo($input);

        if(!empty($pay_order['code'])){

            $this->errorMsg($pay_order['msg']);
        }

        $order_insert = [

            'uniacid'      => $this->_uniacid,

            'pay_type'     => 1,

            'order_code'   => orderCode(),

            'user_id'      => $this->getUserId(),

            'land_id'      => $input['land_id'],

            'pay_price'    => $pay_order['pay_price'],

            'get_integral' => $pay_order['pay_price'],

            'init_price'    => $pay_order['init_price'],

            'farmer_id'    => $pay_order['land']['farmer_id'],

            'goods_name'   => $pay_order['land']['title'],

            'goods_cover'  => $pay_order['land']['cover'],

            'massif_id'    => $input['massif_id'],

            'spe_id'       => $input['spe_id'],

            'spe_name'     => $pay_order['spe']['spe_name'],

            'area'         => $pay_order['spe']['area'],

            'land_price'   => $pay_order['land_price'],

            'seed_price'   => $pay_order['seed_price'],

            'cycle'        => $input['cycle'],
            //到期时间
            'end_time'     => $input['cycle']*86400+time(),

            'rent_mobile'  => $input['rent_mobile'],

            'rent_user_name'=> $input['rent_user_name'],

            'massif_price' => $pay_order['massif']['price'],

            'massif_title' => $pay_order['massif']['title'],

            'total_massif_price' => $pay_order['total_massif_price'],
            //
            'over_time'  => time()+$this->_config['over_time']*60,

            'send_type'  => $input['send_type'],

            'text'       => $input['text'],

            'balance'    => $input['pay_model']==2?$pay_order['pay_price']:0,

            'pay_model'  => $input['pay_model'],

            'coupon_id'    => $pay_order['coupon_id'],

            'discount_price' => $pay_order['coupon_discount'],

        ];

        Db::startTrans();

        $res = $order_model->dataAdd($order_insert);

        if($res==0){

            Db::rollback();

            $this->errorMsg('下单失败');

        }
        $order_id = $order_model->getLastInsID();
        //加销量
        $this->model->where(['id'=>$input['land_id']])->update(['sale_num'=>Db::Raw('sale_num+1')]);

        $input['uniacid'] = $this->_uniacid;

        $res = $address_model->orderAddressAdd($input['address_id'],$order_id,$input['send_type'],1,$input);

        if($res==0){

            Db::rollback();

            $this->errorMsg('下单失败');

        }

        if(!empty($pay_order['seed'])){

            $res = $land_seed_model->orderSeedAdd($pay_order['seed'],$order_id);

            if($res==0){

                Db::rollback();

                $this->errorMsg('下单失败');

            }
        }
        //使用优惠券
        if(!empty($pay_order['coupon_id'])){

            $coupon_record_model = new CouponRecord();

            $coupon_record_model->couponUse($pay_order['coupon_id'],$order_id,2);

        }

        Db::commit();
        //如果是0元
        if($order_insert['pay_price']<=0){

            $order_model->orderResult($order_insert['order_code'],$order_insert['order_code']);

            return $this->success(true);
        }
        //余额支付
        if($input['pay_model']==2){

            $user_model = new \app\farm\model\User();

            $user_balance= $user_model->where(['id'=>$this->getUserId()])->value('balance');

            if($user_balance<$order_insert['pay_price']){

                $this->errorMsg('余额不足');
            }

            $order_model->orderResult($order_insert['order_code'],$order_insert['order_code']);

            return $this->success(true);

        }elseif ($input['pay_model']==3){

            $pay_model = new PayModel($this->payConfig());

            $jsApiParameters = $pay_model->aliPay($order_insert['order_code'],$order_insert['pay_price'],'土地商品');

            $arr['pay_list']= $jsApiParameters;

            $arr['order_code']= $order_insert['order_code'];

        }else{
            //微信支付
            $pay_controller = new \app\shop\controller\IndexWxPay($this->app);
            //支付
            $jsApiParameters= $pay_controller->createWeixinPay($this->payConfig(),$this->getUserInfo()['openid'],$this->_uniacid,"土地订单",['type' => 'Land' , 'out_trade_no' => $order_insert['order_code']],$order_insert['pay_price']);

            $arr['pay_list']= $jsApiParameters;

        }


        return $this->success($arr);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-20 10:17
     * @功能说明:土地重新下单
     */
    public function landRePayOrder(){

        $input = $this->_input;

        $order_model = new LandOrder();

        $order_insert = $order_model->dataInfo(['id'=>$input['id']]);

        if($order_insert['pay_type']!=1){

            $this->errorMsg('订单状态错误');
        }
        //余额支付
        if($order_insert['pay_model']==2){

            $user_model = new \app\farm\model\User();

            $user_balance= $user_model->where(['id'=>$this->getUserId()])->value('balance');

            if($user_balance<$order_insert['pay_price']){

                $this->errorMsg('余额不足');
            }

            $order_model->orderResult($order_insert['order_code'],$order_insert['order_code']);

            return $this->success(true);

        }elseif ($order_insert['pay_model']==3){

            $pay_model = new PayModel($this->payConfig());

            $jsApiParameters = $pay_model->aliPay($order_insert['order_code'],$order_insert['pay_price'],'土地商品');

            $arr['pay_list']= $jsApiParameters;

            $arr['order_code']= $order_insert['order_code'];

        }else{
            //微信支付
            $pay_controller = new \app\shop\controller\IndexWxPay($this->app);
            //支付
            $jsApiParameters= $pay_controller->createWeixinPay($this->payConfig(),$this->getUserInfo()['openid'],$this->_uniacid,"土地订单",['type' => 'Land' , 'out_trade_no' => $order_insert['order_code']],$order_insert['pay_price']);

            $arr['pay_list']= $jsApiParameters;

        }

        return $this->success($arr);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-01-06 18:03
     * @功能说明:种子列表
     */
    public function seedList(){

        $input = $this->_param;

        $land_text_model = new LandText();

        $obj = $land_text_model->where(['land_id'=>$input['land_id'],'type'=>2])->column('obj_id');

        $dis[] = ['a.status','=',1];

        $dis[] = ['a.id','in',$obj];
        //查询当前季节
        if(!empty($input['season'])){

            $month  = date('m',time());

            $season = ceil($month/3);

            $dis[]  = ['b.season','=',$season];
        }

        $seed_model = new Seed();
        //种子
        $data = $seed_model->indexDataList($dis,$input['sort']);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-01-06 18:17
     * @功能说明:种子详情
     */
    public function seedInfo(){

        $input = $this->_param;

        $seed_model = new Seed();

        $dis = [

            'id' => $input['id']
        ];
        //种子
        $data = $seed_model->dataInfo($dis);

        $land_text_model = new LandText();

        $source_model    = new Source();

        $source_id = $land_text_model->where(['land_id'=>$input['land_id'],'obj_id'=>$input['id'],'type'=>2])->value('source_id');

        $data['source'] = $source_model->dataInfo(['id'=>$source_id]);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-28 11:26
     * @功能说明:用户下单列表
     */
    public function orderList(){


        $input = $this->_param;

        $order_model  = new LandOrder();

        $farmer_model = new Farmer();

        $eva_model    = new Evaluate();

        $order_model->orderInit($this->getUserId());

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['user_id','=',$this->getUserId()];

        if(!empty($input['pay_type'])){

            $dis[] = ['pay_type','=',$input['pay_type']];

        }

        if(!empty($input['order_code'])){

            $dis[] = ['order_code','like','%'.$input['order_code'].'%'];

        }

        $data = $order_model->dataList($dis);

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                $v['farmer_info'] = $farmer_model->dataInfo(['id'=>$v['farmer_id']],'title');

                $dis = [

                    'user_id' => $this->getUserId(),

                    'order_id'=> $v['id'],

                    'type'    => 2
                ];

                $have_eva = $eva_model->where($dis)->where('status','>',-1)->find();
                //是否已经评价
                $v['have_eva'] = !empty($have_eva)?1:0;

                $v['end_time'] = date('Y-m-d H:i:s',$v['end_time']);

            }
        }

        $arr = [2=>'rent_count',3=>'send_count'];

        foreach ($arr as $key => $value){

            $where = [

                'user_id' => $this->getUserId(),

                'pay_type'=> $key
            ];

            $data['count'][$value] = $order_model->where($where)->count();

        }

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-28 11:52
     * @功能说明:用户下单详情
     */
    public function orderInfo(){

        $input = $this->_param;

        $farmer_model = new Farmer();

        $dis = [

            'id' => $input['id']
        ];

        $order_model = new LandOrder();

        $seed_model  = new LandOrderSeed();

        $data = $order_model->dataInfo($dis);

        $data['over_time'] -= time();

        $data['time_text']   = date('Y-m-d H:i',$data['pay_time']).'~'.date('Y-m-d H:i',$data['end_time']);

        $data['create_time'] = date('Y-m-d H:i:s',$data['create_time']);

        $data['pay_time']    = $data['pay_time']>0?date('Y-m-d H:i:s',$data['pay_time']):0;

        $data['end_time']    = $data['end_time']>0?date('Y-m-d H:i:s',$data['end_time']):0;

        $data['cancel_time'] = $data['cancel_time']>0?date('Y-m-d H:i:s',$data['cancel_time']):0;
        //农场信息
        $data['farmer_info'] = $farmer_model->dataInfo(['id'=>$data['farmer_id']]);
        //是否可以配送
        $data['can_send'] = $data['pay_type']>1&&$data['end_time']<time()?1:0;

        $eva_model = new Evaluate();

        $dis = [

            'user_id' => $this->getUserId(),

            'order_id'=> $input['id'],

            'type'    => 2
        ];

        $have_eva = $eva_model->where($dis)->where('status','>',-1)->find();
        //是否已经评价
        $data['have_eva'] = !empty($have_eva)?1:0;
        //种子列表
        $data['seed'] = $seed_model->orderSeed($data['id']);

        $address_order_model  = new OrderAddress();

        $address_model        = new Address();

        $data['address_info'] = $address_order_model->dataInfo(['order_id'=>$input['id'],'type'=>1]);

        if(!empty($data['address_info'])){

            $data['address_info'] = $address_model->dataInfo(['id'=>$data['address_info']['address_id']]);

        }

        $mac_model = new Machine();
        //仪器详情
        $data['machine_info'] = $mac_model->getDataInfo($data['land_id']);

        $send_tmpl_id = $this->model->where(['id'=>$data['land_id']])->value('send_tmpl_id');

        $freightTemplate_model = new FreightTemplate();

        $data['send_tmpl'] = $freightTemplate_model->dataInfo(['id'=>$send_tmpl_id]);

        return $this->success($data);

    }



    /**
     * @author chenniang
     * @DataTime: 2022-01-10 13:42
     * @功能说明:
     */
    public function cancelOrder(){

        $input = $this->_input;

        $order_model = new LandOrder();

        $dis = [

            'id' => $input['id']
        ];

        $order = $order_model->dataInfo($dis);

        if($order['pay_type']!=1){

            $this->errorMsg('订单状态错误');
        }

        $res = $order_model->cancelOrder($order);

        if(!empty($res['code'])){

            $this->errorMsg($res['msg']);
        }

        return $this->success($res);

    }




    /**
     * @author chenniang
     * @DataTime: 2022-02-08 13:53
     * @功能说明:申请认养订单发货
     */
    public function sendOrderApply(){

        $input = $this->_input;

        $order_model = new LandOrder();

        $farmer_model= new Farmer();

        $send_order_model = new SendOrder();

        $address_model    = new Address();

        $config_model     = new Config();

        $order = $order_model->dataInfo(['id'=>$input['order_id']]);

        if($order['pay_type']<2){

            $this->errorMsg('订单未支付');

        }

        $times = $send_order_model->where(['order_id'=>$input['order_id'],'type'=>2])->where('pay_time','>',0)->count();

        $farmer= $farmer_model->dataInfo(['id'=>$order['farmer_id'],'status'=>2]);

        if(empty($farmer)){

            $this->errorMsg('农场主未找到');
        }

        $send_price = $distance = $send_tmpl_type =  0;
        //快递
        if($input['send_type']==2){

            $address = $address_model->dataInfo(['id'=>$input['address_id']]);

            if(empty($address)){

                $this->errorMsg('地址信息未找到');

            }

            $land = $this->model->dataInfo(['id'=>$order['land_id']]);

            if(empty($land)){

                $this->errorMsg('土地信息被删除');

            }

            $goods_info = [

                'send_tmpl_id' => $land['send_tmpl_id'],

                'goods_num'    => $input['send_num'],

                'total_weight' => $input['send_num'],
            ];

            $send_data = $config_model->getGoodsSendPrice($input['address_id'],$goods_info,2,2);

            if(!empty($send_data['code'])){

                $this->errorMsg($send_data['msg']);
            }

            $send_price = $send_data['price'];

            $send_tmpl_type = $send_data['type'];

        }else{

            $address['user_name'] = $input['user_name'];

            $address['mobile']    = $input['mobile'];

        }

        $order_insert = [

            'uniacid'    => $this->_uniacid,

            'order_id'   => $input['order_id'],

            'user_id'    => $order['user_id'],

            'farmer_id'  => $order['farmer_id'],

            'pay_price'  => $send_price,

            'get_integral'=> $send_price,

            'order_code' => orderCode(),

            'text'       => $input['text'],
            //
            'over_time'  => time()+$this->_config['over_time']*60,

            'type'       => 2,

            'start_time' => $input['start_time'],

            'end_time'   => $input['end_time'],

            'distance'   => $distance,

            'times'      => $times+1,

            'send_type'  => $input['send_type'],

            'address'    => $input['send_type']==2?$address['address'].$address['address_info']:'',

            'user_name'  => $address['user_name'],

            'mobile'     => $address['mobile'],

            'lng'        => $input['send_type']==2?$address['lng']:'',

            'lat'        => $input['send_type']==2?$address['lat']:'',

            'balance'    => $input['pay_model']==2?$send_price:0,

            'pay_model'  => $input['pay_model'],

            'goods_num'  => isset($send_tmpl_type)&&$send_tmpl_type==1?$input['send_num']:0,

            'weight'     => isset($send_tmpl_type)&&$send_tmpl_type==2?$input['send_num']:0,

        ];

        $res = $send_order_model->dataAdd($order_insert);

        if($res==0){

            $this->errorMsg('申请配送失败');
        }
        //如果是0元
        if($order_insert['pay_price']<=0){

            $send_order_model->orderResult($order_insert['order_code'],$order_insert['order_code']);

            return $this->success(true);
        }
        //余额支付
        if($input['pay_model']==2){

            $user_model = new \app\farm\model\User();

            $user_balance= $user_model->where(['id'=>$this->getUserId()])->value('balance');

            if($user_balance<$order_insert['pay_price']){

                $this->errorMsg('余额不足');
            }

            $send_order_model->orderResult($order_insert['order_code'],$order_insert['order_code']);

            return $this->success(true);

        }elseif ($input['pay_model']==3){

            $pay_model = new PayModel($this->payConfig());

            $jsApiParameters = $pay_model->aliPay($order_insert['order_code'],$order_insert['pay_price'],'配送订单');

            $arr['pay_list']= $jsApiParameters;

            $arr['order_code']= $order_insert['order_code'];

        }else{
            //微信支付
            $pay_controller = new \app\shop\controller\IndexWxPay($this->app);
            //支付
            $jsApiParameters= $pay_controller->createWeixinPay($this->payConfig(),$this->getUserInfo()['openid'],$this->_uniacid,"认养配送订单",['type' => 'ClaimSend' , 'out_trade_no' => $order_insert['order_code']],$order_insert['pay_price']);

            $arr['pay_list']= $jsApiParameters;
        }

        return $this->success($arr);

    }




    /**
     * @author chenniang
     * @DataTime: 2022-02-08 15:23
     * @功能说明:配送订单下单详情
     */
    public function sendOrderPayInfo(){

        $input  = $this->_param;

        $order_model = new LandOrder();

        $farmer_model= new Farmer();

        $config_model= new Config();

        $address_model = new Address();

        $order = $order_model->dataInfo(['id'=>$input['order_id']]);

        $farmer= $farmer_model->dataInfo(['id'=>$order['farmer_id']]);

        if(empty($farmer)){

            $this->errorMsg('农场主未找到');
        }

//        $address = $address_model->dataInfo(['id'=>$input['address_id']]);
//
//        if(empty($address)){
//
//            $this->errorMsg('地址信息未找到');
//
//        }

        $land = $this->model->dataInfo(['id'=>$order['land_id']]);

        if(empty($land)){

            $this->errorMsg('土地信息被删除');

        }

        $freightTemplate_model = new FreightTemplate();

        $data['send_tmpl'] = $freightTemplate_model->dataInfo(['id'=>$land['send_tmpl_id']]);
        //计算运费
        if(!empty($input['send_num'])){

            $goods_info = [

                'send_tmpl_id' => $land['send_tmpl_id'],

                'goods_num'    => $input['send_num'],

                'total_weight' => $input['send_num'],
            ];

            $data['send_price'] = $config_model->getGoodsSendPrice($input['address_id'],$goods_info,2);

            if(!empty($data['send_price']['code'])){

                $this->errorMsg($data['send_price']['msg']);
            }

        }

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-02-08 17:14
     * @功能说明:配送订单列表
     */
    public function userSendOrderList(){

        $input = $this->_param;

        $send_order_model = new SendOrder();

        $data = $send_order_model->orderSendOrder($input['land_order_id'],2);

        return $this->success($data);

    }

    /**
     * @author chenniang
     * @DataTime: 2022-02-09 14:00
     * @功能说明:配送订单详情
     */
    public function sendOrderInfo()
    {

        $input = $this->_param;

        $send_order_model = new SendOrder();

        $land_order_model = new LandOrder();

        $seed_model       = new LandOrderSeed;

        $send_order = $send_order_model->dataInfo(['id' => $input['id']]);

        $send_order['create_time'] = date('Y-m-d H:i:s', $send_order['create_time']);

        $send_order['time_text'] = date('Y-m-d H:i', $send_order['start_time']) . '~' . date('H:i', $send_order['end_time']);

        $send_order['pay_time'] = $send_order['pay_time'] > 0 ? date('Y-m-d H:i:s', $send_order['pay_time']) : 0;

        $send_order['send_time'] = $send_order['send_time'] > 0 ? date('Y-m-d H:i:s', $send_order['send_time']) : 0;

        $send_order['receiving_time'] = $send_order['receiving_time'] > 0 ? date('Y-m-d H:i:s', $send_order['receiving_time']) : 0;

        $send_order['refund_time'] = $send_order['refund_time'] > 0 ? date('Y-m-d H:i:s', $send_order['refund_time']) : 0;

        $send_order['land_order'] = $land_order_model->dataInfo(['id' => $send_order['order_id']]);

        $send_order['land_order']['end_time'] = date('Y-m-d H:i:s',$send_order['land_order']['end_time']);
        //种子列表
        $send_order['land_order']['seed'] = $seed_model->orderSeed($send_order['order_id']);

        return $this->success($send_order);

    }



    /**
     * @author chenniang
     * @DataTime: 2022-02-08 17:57
     * @功能说明:配送订单退款
     */
    public function sendOrderRefund(){

        $input = $this->_input;

        $send_order_model = new SendOrder();

        $send_order = $send_order_model->dataInfo(['id'=>$input['id']]);

        if(empty($send_order)){

            $this->errorMsg('订单未找到');
        }

        if($send_order['pay_type']!=2){

            $this->errorMsg('订单状态错误');
        }

        Db::startTrans();

        $res = $send_order_model->refundCash($send_order,$this->payConfig($this->_uniacid));

        if(!empty($res['code'])){

            $this->errorMsg($res['msg']);
        }

        $send_order_model->dataUpdate(['id'=>$send_order['id']],['refund_time'=>time(),'pay_type'=>-1,'refund'=>1]);
        //增加财务流水
        $fin_water_model = new FinanceWater();

        $res = $fin_water_model->addWater($input['id'],13,1,1);

        if($res==0){

            Db::rollback();

            $this->errorMsg('退款失败1');
        }

        $res = $fin_water_model->dataUpdate(['order_id'=>$send_order['id'],'type'=>14],['cash_status'=>1]);

        if($res==0){

            Db::rollback();

            $this->errorMsg('退款失败3');
        }

        Db::commit();

        return $this->success(true);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-02-09 10:24
     * @功能说明:配送订单收货
     */
    public function sendOrderReceiving(){

        $input = $this->_input;

        $send_order_model = new SendOrder();

        $send_order = $send_order_model->dataInfo(['id'=>$input['id']]);

        if(empty($send_order)){

            $this->errorMsg('订单未找到');
        }

        if($send_order['pay_type']!=3){

            $this->errorMsg('订单状态错误');
        }

        $res = $send_order_model->sendOrderReceiving($send_order);

        if(!empty($res['code'])){

            $this->errorMsg($res['msg']);
        }

        return $this->success(true);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-07-10 00:40
     * @功能说明:可用的优惠券列表
     */
    public function couponList(){

        $input = $this->_input;

        $coupon_model = new Coupon();

        $coupon_record_model = new CouponRecord();

        $order_model    = new LandOrder();

        $order_info = $order_model->payOrderInfo($input);

        $coupon_id = $coupon_model->canUseCoupon($this->getUserId(),$order_info['pay_price'],2);

        $data = $coupon_record_model->where('id','in',$coupon_id)->order('id desc')->paginate(10)->toArray();

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                $v['start_time'] = date('Y.m.d H:i',$v['start_time']).' - '.date('Y.m.d H:i',$v['end_time']);

            }

        }

        return $this->success($data);

    }





}
